package per.example.arpproject.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import per.example.arpproject.entity.Script;
import per.example.arpproject.entity.ScriptDetail;
import per.example.arpproject.model.ApiResult;
import per.example.arpproject.service.ScriptService;
import per.example.arpproject.utils.FileUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author yaozhang Li
 * @ TO DO
 * @createTime 2023/7/6 8:46
 **/
@RestController
@Api("文件上传测试接口")
@RequestMapping(value = "/script",method = RequestMethod.POST)
@Slf4j
public class ScriptController {

    @Autowired
    ScriptService service;

    @ApiOperation("获取所有脚本文件夹")
    @RequestMapping("/list")
    public ApiResult<List<Script>> list() {
        List<Script> list = service.getAllScript();
        return ApiResult.success(list);
    }

    @ApiOperation("新增脚本文件")
    @RequestMapping("/add")
    public ApiResult add(Script script) {
        service.addScript(script);
        return ApiResult.success();
    }

    @ApiOperation("更新脚本文件")
    @RequestMapping("/update")
    public ApiResult update(Script script) {
        service.updateScript(script);
        return ApiResult.success();
    }

    @ApiOperation("删除脚本文件夹")
    @RequestMapping("/delete")
    private ApiResult delete(Long id, Boolean deleteFile){
        service.deleteScript(id,deleteFile);
        return ApiResult.success();
    }

    @ApiOperation("获取脚本详细")
    @RequestMapping("/getDetailById")
    private ApiResult<ScriptDetail> getDetailById(Long id){
        ScriptDetail detailById = service.getDetailById(id);
        return ApiResult.success(detailById);
    }

    @ApiOperation("保存脚本")
    @RequestMapping("/updateDetailById")
    private ApiResult updateDetailById(ScriptDetail scriptDetail){
        service.updateDetailById(scriptDetail);
        return ApiResult.success();
    }

    @ApiOperation("为脚本文件上传图片")
    @RequestMapping("/uploadPict")
    public ApiResult<String> uploadPict(Long id, MultipartFile file) throws IOException {
        String filePath = service.uploadPict(id, file);
        ApiResult result = ApiResult.success();
        result.setData(filePath);
        return result;
    }

    @ApiOperation("导出脚本")
    @RequestMapping("/export")
    public void exprot(Long id, String withEnv, HttpServletRequest request, HttpServletResponse response) throws IOException {
        service.exportScript(id,withEnv,request,response);
    }

    @ApiOperation("导入脚本")
    @RequestMapping("/importScript")
    public ApiResult importScript(MultipartFile file, String name){
        service.importScript(file, name);
        return ApiResult.success();
    }


}
